package com.yisu.auction.manager.website.controller.baseInfo;


import com.google.common.collect.Lists;
import com.yisu.auction.ewf.common.enums.ResponseCode;
import com.yisu.auction.ewf.common.web.EasyUIGrid;
import com.yisu.auction.ewf.common.web.JsonResult;
import com.yisu.auction.ewf.entity.AppSignGoods;
import com.yisu.auction.ewf.entity.AppSignManager;
import com.yisu.auction.ewf.entity.GoodsInfo;
import com.yisu.auction.ewf.entity.GoodsType;
import com.yisu.auction.manager.core.service.*;
import com.yisu.auction.manager.domain.request.appSignPackage.AppSignGoodsRequest;
import com.yisu.auction.manager.domain.request.baseInfoPackage.GoodsCategoryRequest;
import com.yisu.auction.manager.domain.request.baseInfoPackage.GoodsInfoRequest;
import com.yisu.auction.manager.domain.util.RedisUtil;
import com.yisu.auction.manager.domain.vo.baseInfoPackageVos.AppSignGoodsVo;
import com.yisu.auction.manager.domain.vo.baseInfoPackageVos.GoodsExpandInfoVo;
import com.yisu.auction.manager.website.controller.BaseController;
import org.apache.calcite.linq4j.Linq4j;
import org.apache.calcite.linq4j.function.Predicate1;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.net.URLDecoder;
import java.util.List;

/**
 * author: 赵杰
 * Business Description：商品信息相关业务
 * createTime：2017/8/10
 */
@RequestMapping(value = "/goodsInfo")
@Controller
public class GoodsInfoController extends BaseController {
    @Resource
    private IGoodsInfoService iGoodsInfoService;
    @Resource
    private IGoodsTypeService iGoodsTypeService;
    @Resource
    private IPrizeService iPrizeService;
    @Resource
    private IAppSignGoodsService iAppSignGoodsService;
    @Resource
    private IAppSignService iAppSignService;

    /**
     * author: 赵杰
     * Business Description：获取商品信息首页
     *
     * @param model
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/index.htm")
    public String index(Model model, HttpServletRequest request, GoodsInfoRequest goodsInfoRequest) throws Exception {
        try {
            if (goodsInfoRequest.getKeyword() != null) {
                goodsInfoRequest.setKeyword(URLDecoder.decode(goodsInfoRequest.getKeyword(), "utf-8").trim());
            }
            EasyUIGrid<GoodsExpandInfoVo> goodsInfoEasyUIGrid = iGoodsInfoService.getGoodsInfoByPage(goodsInfoRequest);
            model.addAttribute("GoodsInfoVos", goodsInfoEasyUIGrid.getRows());
            model.addAttribute("currentPage", goodsInfoRequest.getPage());
            model.addAttribute("totalPages", (goodsInfoRequest.getTotalRecords() / goodsInfoRequest.getRows()) + 1);
            model.addAttribute("keyword", goodsInfoRequest.getKeyword());
            model.addAttribute("goodsTypeId", goodsInfoRequest.getGoodsTypeId());
            model.addAttribute("appPackgeSign", goodsInfoRequest.getAppPackgeSign());
            //获取商品分类列表
            GoodsCategoryRequest goodsCategoryRequest=new GoodsCategoryRequest();
            goodsCategoryRequest.setPage(1);
            goodsCategoryRequest.setRows(200);
            EasyUIGrid<GoodsType> goodsTypeEasyUIGrid = iGoodsTypeService.getGoodsTypeByPage(goodsCategoryRequest);
            model.addAttribute("GoodsTypeVos", goodsTypeEasyUIGrid.getRows());

            List<AppSignManager> appSignManagers = iAppSignService.getAppSignManagerList();
            model.addAttribute("AppSignManagers", appSignManagers);
        } catch (Exception e) {
            logger.error("获取商品信息首页异常：" + e.getMessage());
        }
        return "/baseInfo/goodsInfo/index";
    }

    /**
     * author: 赵杰
     * Business Description：商品信息编辑页面初始
     *
     * @param model
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/modify.htm")
    public String modify(Model model, HttpServletRequest request, GoodsInfoRequest goodsInfoRequest) throws Exception {
        GoodsInfo goodsInfo = new GoodsInfo();
        goodsInfo.setId(Long.valueOf(0));
        List<AppSignGoodsVo> appSignGoodsVos = Lists.newArrayList();
        try {
            if (goodsInfoRequest.getId() != 0) {
                goodsInfo = iGoodsInfoService.getGoodsInfo(goodsInfoRequest.getId());
                AppSignGoodsRequest appSignGoodsRequest = new AppSignGoodsRequest();
                appSignGoodsRequest.setGoodsId(goodsInfoRequest.getId());
                List<AppSignManager> appSignManagers = iAppSignService.getAppSignManagerList();
                List<AppSignGoods> appSignGoods = iAppSignGoodsService.getAppSignGoodsListByGoodsId(appSignGoodsRequest);
                if (!CollectionUtils.isEmpty(appSignManagers)) {
                    for (final AppSignManager appSignManager : appSignManagers) {
                        AppSignGoodsVo appSignGoodsVo = new AppSignGoodsVo();
                        appSignGoodsVo.setAppPackageSign(appSignManager.getAppPackageSign());
                        appSignGoodsVo.setDescription(appSignManager.getDescription());
                        appSignGoodsVo.setCheckedStatus(0);
                        Integer isSelected = Linq4j.asEnumerable(appSignGoods).where(new Predicate1<AppSignGoods>() {
                            @Override
                            public boolean apply(AppSignGoods appSignGoods) {
                                return appSignGoods.getAppPackageSign().equals(appSignManager.getAppPackageSign());
                            }
                        }).count();
                        if (isSelected > 0) {
                            appSignGoodsVo.setCheckedStatus(1);
                        }
                        appSignGoodsVos.add(appSignGoodsVo);
                    }
                }
            }
            model.addAttribute("appSignGoodsVos", appSignGoodsVos);
            model.addAttribute("goodsInfoVo", goodsInfo);
            //获取商品分类列表
            GoodsCategoryRequest goodsCategoryRequest=new GoodsCategoryRequest();
            goodsCategoryRequest.setPage(1);
            goodsCategoryRequest.setRows(200);
            EasyUIGrid<GoodsType> goodsTypeEasyUIGrid = iGoodsTypeService.getGoodsTypeByPage(goodsCategoryRequest);
            model.addAttribute("GoodsTypeVos", goodsTypeEasyUIGrid.getRows());

        } catch (Exception e) {
            logger.error("商品信息编辑页面初始异常：" + e.getMessage());
        }
        return "/baseInfo/goodsInfo/modify";
    }

    /**
     * author: 赵杰
     * Business Description：商品档案编辑操作
     *
     * @param goodsInfo 商品档案基本信息
     * @return 状态
     */
    @RequestMapping(value = "/modifyAction.json", produces = "application/json; charset=utf-8")
    @ResponseBody
    public JsonResult modifyAction(HttpServletRequest request, GoodsInfo goodsInfo) {
        try {
            String appSign=request.getParameter("appSignTemp");
            if (iGoodsInfoService.modifyGoodsInfo(goodsInfo,appSign)) {
                return new JsonResult();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JsonResult(ResponseCode.FAILURE.code, "网络君泡妞去了！");
    }

    /**
     * author: 赵杰
     * Business Description：删除商品档案信息
     *
     * @param goodsInfoRequest 含主键ID
     * @return 状态
     */
    @RequestMapping(value = "/deleteAction.json", produces = "application/json; charset=utf-8")
    @ResponseBody
    public JsonResult deleteAction(HttpServletRequest request, GoodsInfoRequest goodsInfoRequest) {
        try {
            if (goodsInfoRequest.getId() == 0) {
                return new JsonResult(ResponseCode.FAILURE.code, "参数不完整！");
            }
            if (iGoodsInfoService.deleteGoodsInfo(goodsInfoRequest.getId())) {
                return new JsonResult();
            }
        } catch (Exception e) {
            logger.error("删除商品档案信息异常：" + e.getMessage());
        }
        return new JsonResult(ResponseCode.FAILURE.code, "网络君泡妞去了！");
    }


    /**
     * author: 赵杰
     * Business Description：开始商品新的一期夺宝
     *
     * @param goodsInfoRequest 含主键ID
     * @return 状态
     */
    @RequestMapping(value = "/startSnatchTreasureAction.json", produces = "application/json; charset=utf-8")
    @ResponseBody
    public JsonResult startSnatchTreasureAction(HttpServletRequest request, GoodsInfoRequest goodsInfoRequest) {
        try {
            if (goodsInfoRequest.getId() == 0) {
                return new JsonResult(ResponseCode.FAILURE.code, "参数不完整！");
            }
            if (iPrizeService.startSnatchTreasure(goodsInfoRequest.getId())) {
                return new JsonResult();
            }
        } catch (Exception e) {
            logger.error("开始商品新的一期夺宝异常：" + e.getMessage());
        }
        return new JsonResult(ResponseCode.FAILURE.code, "网络君泡妞去了！");
    }
}
